<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//商品属性控制器

class Attribute extends Admin_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model('admin/goodstype_model');
		$this->load->model('admin/attribute_model');
        $this->load->library('form_validation');
	}

	public function index(){
	    $type = $this->input->get('type_id');
		$data['types'] = $this->goodstype_model->list_goodstype();
		$data['attrs'] = $this->attribute_model->list_attribute($type);
		$data['type_id'] = $type;
        $data['nums']=  $this->attribute_model->count_attrs($type);
		$this->load->view('attribute_list.html',$data);
	}

	public function add(){
		#获取商品类型信息
		$data['goodstypes'] = $this->goodstype_model->get_all_types();
        $data['type_id'] = $this->input->get('type_id');
		$this->load->view('attribute_add.html',$data);
	}

	public function edit(){
		$this->load->view('attribute_edit.html');
	}

	#添加属性
	public function insert(){
        #设置验证规则
        $this->form_validation->set_rules('attr_name', '属性名称', 'trim|required');
        $this->form_validation->set_rules('type_id', '请选择所属商品类型', 'trim|required|is_natural_no_zero');
        if ($this->form_validation->run() == false) {
            # code...
            $data['message'] = validation_errors();
            $data['wait'] = 5;
            $data['url'] = base_url() . 'admin/attribute/index';
            $this->load->view('message.html', $data);
        }else{
            $data['attr_name'] = $this->input->post('attr_name',true);
            $data['type_id'] = $this->input->post('type_id',true);
            //$data['attr_type'] = $this->input->post('attr_type');
            $data['attr_input_type'] = $this->input->post('attr_input_type');
            $data['attr_value'] = $this->input->post('attr_value');
            //$data['sort_order'] = $this->input->post('sort_order');
            if ($this->attribute_model->add_attrs($data)) {
                # ok
                $data['message'] = '添加属性成功';
                $data['url'] = base_url().'admin/attribute/index';
                $data['wait'] = 5;
                $this->load->view('message.html',$data);
            } else {
                # error
                $data['message'] = '添加属性失败';
                $data['url'] = base_url().'admin/attribute/add';
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }
        }

		
	}
}